package com.yxhl.zoume.core.user.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.view.FeedbackView;
import com.yxhl.zoume.data.http.rest.param.feedback.FeedbackParam;
import com.yxhl.zoume.data.http.rest.response.feedback.FeedbackResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.usercenter.FeedbackUseCase;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/11/3 .
 */

public class FeedbackPresenter extends BasePresenter implements IPresenter {
    private FeedbackView mFeedbackView;
    @Inject
    FeedbackUseCase mFeedbackUseCase;

    @Inject
    public FeedbackPresenter(Context context) {
        super(context);
    }

    public void commitFeedback(String stFeedback) {
        FeedbackParam feedbackParam = new FeedbackParam(stFeedback);
        feedbackParam
                .setMobile(getMobile())
                .setToken(getToken());
        mFeedbackUseCase
                .execute(feedbackParam,new ZMSubscriber<FeedbackResponse>(){
                    @Override
                    public void onNext(FeedbackResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(null != baseResponse){
                            boolean isSuccess = baseResponse.isSucc();
                            if (isSuccess){
                                mFeedbackView.renderFeedbackOnSuccess();
                            }else{
                                mFeedbackView.renderFeedbackOnFailure(baseResponse.getResultMessage());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mFeedbackView.renderFeedbackOnError(e.getMessage());
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }

    public void detectionLogin (){
        FeedbackParam feedbackParam = new FeedbackParam("检测登陆");
        feedbackParam
                .setMobile(getMobile())
                .setToken(getToken());
        mFeedbackUseCase
                .execute(feedbackParam,new ZMSubscriber<FeedbackResponse>(){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onNext(FeedbackResponse baseResponse) {
                        super.onNext(baseResponse);
                    }
                });
    }

    @Override
    public void attachView(BaseView view) {
        mFeedbackView = (FeedbackView) view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mFeedbackUseCase.unsubscribe();
    }
}
